package com.jimmy.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jimmy.reggie.common.BaseContext;
import com.jimmy.reggie.common.R;
import com.jimmy.reggie.entity.ShoppingCart;
import com.jimmy.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        log.info(shoppingCart.toString());
        /*
           这里的id是存在ThreadLocal里面的，所以通过工具类来拿
           所以要区分session和工具类
         */
        Long userId  = BaseContext.getCurrentID();
        shoppingCart.setUserId(userId);
        //拿菜品对象
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        //先判断添加的是不是菜品
        if (dishId!=null){
              shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
               shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //在判断购物车是否存在
        ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if (one!=null){
            //购物车存在，就在原来数量的基础上加一
            Integer number = one.getNumber();
            one.setNumber(number+1);
             //更新购物车信息
            shoppingCartService.updateById(one);
        }else {
            //购物车不存在，那么就把当前的shoppingCart对象添加到表中
             shoppingCart.setNumber(1);
             shoppingCart.setCreateTime(LocalDateTime.now());
             shoppingCartService.save(shoppingCart);
             one=shoppingCart;
        }
        return R.success(one);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> getlist(){
        log.info("开始查询咯");
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentID());
        shoppingCartLambdaQueryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> cleanShoppingCart(){
        log.info("开始删除");
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentID());
        boolean remove = shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        if (remove){
         return   R.success("删除成功");
        }
       return R.error("未知错误");
    }

    @PostMapping("/sub")
    public R<List<ShoppingCart>> subShoppingCart(@RequestBody ShoppingCart shoppingCart){
        log.info("减少菜品开始咯");
        Long userId = BaseContext.getCurrentID();
        shoppingCart.setUserId(userId);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        if (dishId!=null){
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        Integer number = one.getNumber();
        if (number>0){
            one.setNumber(number-1);
            //更新购物车信息
            shoppingCartService.updateById(one);
        }
        //做到页面同步删除当前信息
       if (number==1){
           shoppingCartService.removeById(one);
       }
        return  this.getlist();
    }

}
